<?php


namespace app\common\model;


use app\common\lib\ZhyxBeanstalkd;
use app\common\lib\ZhyxMongo;
use catcher\exceptions\FailedException;
use MongoDB\BSON\ObjectId;

class DiseaseTask extends BaseModel
{
    protected $name = 'disease_patient_task';

    public function writeTask($data, $patientInfo, $bind_id, $doctors, $sessionId = ''): array
    {
        $taskInfo = [];
        foreach ($data["steps"] as $k => $v) {
            $task = $v;
            $task["bind_id"] = $bind_id;
            $task["doctors"] = $doctors;
            $task["plan_status"] = 0;
            $task["plan_start_date"] = "";
            $task["disease_name"] = $data["name"];
            $task["disease"] = $data["disease"];
            $task["severity"] = $data["severity"];
            $task["type"] = $data["type"];
            $task["patient_id"] = $patientInfo["patientId"];
            $task["patient_name"] = $patientInfo["name"];
            $task["patient_sex"] = $patientInfo["sex"] === "1" ? "男" : "女";
            $task["task_is_stop"] = 0; //0未停用 1停用
            $task["created_at"] = time();
            $task['creator_id'] = 0;
            $task["create_by"] = '系统自动执行';
            $task["deleted_at"] = 0;
            $result = ZhyxMongo::getInstance()->setCollection('disease_patient_task')->insertOne($task, ["session" => $sessionId]);
            if ($result->getInsertedCount() > 0) {
                $patientInfo["bind_main_id"] = (string)$bind_id;
                $calcPushDate = (new \app\common\business\DiseasePath())->calcPath($patientInfo, $result->getInsertedId(), $sessionId);
                foreach ($calcPushDate as $k => $v) {
                    $calcPushDate[$k]["bind_id"] = (string)$bind_id;
                    $taskInfo[] = $calcPushDate[$k];
                }

            }
        }
        return $taskInfo;
    }

    public function getDiseaseTaskByBindId(string $taskId, $session)
    {

        return ZhyxMongo::getInstance()->setCollection("disease_patient_task")->findOne(["_id" => new ObjectId($taskId)], ["session" => $session]);
    }

    public function tagUnfinished(string $bindId)
    {
        $modifyData = [
            'plan_status' => 3,
            'step_detail.latest_status' => 3
        ];

        return $this->where("bind_id", "=", new ObjectId($bindId))->update([


        ]);
    }


}